package io.renren.common.util;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class BeanUtils extends org.springframework.beans.BeanUtils{
    public static Map<String,Object> beanToMap(Object bean) throws IntrospectionException, InvocationTargetException, IllegalAccessException {

        Map<String,Object> map = new HashMap<>();
        BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass(),Object.class);
        PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();

        for(PropertyDescriptor pd : pds) {
            String key = pd.getName();
            Method getter = pd.getReadMethod();
            Object value = getter.invoke(bean);
            map.put(key, value);
        }

        return map;
    }
}
